package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created by Administrator on 2017/8/18 0018.
 */
@Controller
@RequestMapping(ControllerConstants.BUSINESS)
public class BusinessWController extends WebBaseController {

    @Autowired
    private BusinessService businessService;

    @Autowired
    private BussTypeService bussTypeService;

    @Autowired
    private WaiterService waiterService;

    @Autowired
    private PromotionService promotionService;

    @Autowired
    private WaiterlistService waiterlistService;

    /**
     * 跳转商家页面
     *
     * @return
     */
    @RequestMapping("/findBusinessIndex")
    public String findBusiness() {
        return "/taoshunda/business/business";
    }


    /**
     * 跳转商家审核页面
     *
     * @return
     */
    @RequestMapping("/findAuthstr")
    public String findAuthstr() {
        return "/taoshunda/business/authstr";
    }

    /**
     * 跳转本地服务页面
     *
     * @return
     */
    @RequestMapping("/findWaiterIndex")
    public String findWaiter() {
        return "/taoshunda/business/waiterBusiness";
    }

    /**
     * 跳转未通过商家页面
     *
     * @return
     */
    @RequestMapping("/findNotBusinessIndex")
    public String findNotBusiness() {
        return "/taoshunda/business/notThrough";
    }

    /**
     * 跳转优惠活动页面
     *
     * @return
     */
    @RequestMapping("/findActivityIndex")
    public String findActivity() {
        return "/taoshunda/business/activity";
    }


    /**
     * 跳转推荐商家审核
     *
     * @return
     */
    @RequestMapping("/findAuditingBusiness")
    public String findAuditingBusiness() {
        return "/taoshunda/auditing/auditingBusiness";
    }

    /**
     * 查询商家列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryBusinessList")
    public void queryBusinessList(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                String startCreate = getString(request, "startCreate", "");
                String endCreate = getString(request, "endCreate", "");
                String startTime = getString(request, "startTime", "");
                String endTime = getString(request, "endTime", "");
                Integer cityId = getInt(request, "cityId", 0);
                Integer typeId = getInt(request, "typeId", 0);
                String name = getString(request, "businessName", "");
                return businessService.queryBusinessList(startCreate, endCreate, startTime, endTime, cityId, typeId, name, nowPage, pageSize);
            }
        });
    }

    /**
     * 商家订单查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/businessOrderList")
    public void businessOrderList(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return businessService.businessOrderList(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 商家明细查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/businessOrderDetails")
    public void businessOrderDetails(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return businessService.businessOrderDetails(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 重置商家密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateBusinessPwd")
    public void updateBusinessAccount(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String pwd = getString(request, "pwd", "");
                return businessService.updateBusinessAccount(id, pwd);
            }
        });
    }

    /**
     * 修改商家信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateBusinessInfo")
    public void updateBusinessInfo(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String bussName = getString(request, "bussName", "");
                Integer bussId = getInt(request, "bussId", 0);
                String bussPhone = getString(request, "bussPhone", "");
                String serviceTel = getString(request, "serviceTel", "");
                String address = getString(request, "address", "");
                Integer cityId = getInt(request, "cityId", 0);
                Integer areaId = getInt(request, "areaId", 0);
                String serviceLince = getString(request, "serviceLince", "");
                String bussLicnese = getString(request, "bussLicnese", "");
                String remarks = getString(request, "remarks", "");
                return businessService.updateBusinessInfo(id, bussName, bussId, bussPhone, serviceTel, address, cityId, areaId, serviceLince, bussLicnese, remarks);
            }
        });
    }

    /**
     * 商家禁用、启用
     * 删除
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateStatus")
    public void updateStatus(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer state = getInt(request, "state", 0);
                return businessService.updateStatus(id, state);
            }
        });
    }

    /**
     * 查询审核商家
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryBusinessAudit")
    public void queryBusinessAudit(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String businessName = getString(request, "businessName", "");
                String account = getString(request, "account", "");
                String telephone = getString(request, "telephone", "");
                String cityName = getString(request, "cityName", "");
                String address = getString(request, "address", "");
                return businessService.queryBusinessAudit(businessName, account, telephone, cityName, address, nowPage, pageSize);
            }
        });
    }

    /**
     * 审核商家未通过原因
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateAudit")
    public void updateAudit(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                Integer audit = getInt(request, "audit", 0);
                String reason = getString(request, "reason", "");
                String logo = getString(request, "logo", "");
                String store = getString(request, "store", "");
                String shop = getString(request, "shop", "");
                String legalCardPic = getString(request, "legalCardPic", "");
                String service = getString(request, "service", "");
                String licnese = getString(request, "licnese", "");
                return businessService.updateAudit(id, audit, reason, logo, store, shop, legalCardPic, service, licnese);
            }
        });
    }

    /**
     * 获取分类信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryByTypeId")
    public void queryByTypeId(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                return bussTypeService.queryByTypeId();
            }
        });
    }


    /**
     * 查看优惠券
     *
     * @param request
     * @param response
     */
    @RequestMapping("/examineCoupon")
    public void examineCoupon(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return businessService.examineCoupon(id, nowPage, pageSize);
            }
        });
    }

    /**
     * 查看优惠活动
     *
     * @param request
     * @param response
     */
    @RequestMapping("/examineActivity")
    public void examineActivity(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return businessService.examineActivity(id);
            }
        });
    }

    /**
     * 查询未通过商家
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryNotBusiness")
    public void queryNotBusiness(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String businessName = getString(request, "businessName", "");
                String account = getString(request, "account", "");
                String telephone = getString(request, "telephone", "");
                String cityName = getString(request, "cityName", "");
                String address = getString(request, "address", "");
                return businessService.queryNotBusiness(businessName, account, telephone, cityName, address, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询商家优惠活动
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryBusinessActivity")
    public void queryBusinessActivity(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String businessName = getString(request, "businessName", "");
                return businessService.queryBusinessActivity(businessName, nowPage, pageSize);
            }
        });
    }

    /**
     * 修改商家优惠活动
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateBusinessActivity")
    public void updateBusinessActivity(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String name = getString(request, "name", "");
                return promotionService.updateBusinessActivity(id, name);
            }
        });
    }

    /**
     * 删除商家优惠活动
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deleteActivity")
    public void deleteActivity(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                return promotionService.deleteActivity(id);
            }
        });
    }

    /**
     * 根据城市查询商家
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryBusinessCity")
    public void queryBusinessCity(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "cityId", 0);
                return businessService.queryBusinessCity(id);
            }
        });
    }

    /**
     * 添加商家优惠
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addBusinessActivity")
    public void addBusinessActivity(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer businessId = getInt(request, "businessId", 0);
                String name = getString(request, "name", "");
                return promotionService.addBusinessActivity(businessId, name);
            }
        });
    }

    /**
     * 查询商家的位置列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/companyaddrlist")
    public void companyaddrlist(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String sid = request.getSession().getId();
                int city = Integer.valueOf(request.getSession().getAttribute(sid).toString());
                return waiterlistService.findAllCompanyAtlas(city, key);
            }
        });
    }

    /**
     * 推荐商家审核
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryRecommendBusiness")
    public void queryAuditingBusiness(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String businessName = getString(httpServletRequest, "businessName","");
                return businessService.queryAuditingBusiness(nowPage, pageSize,businessName);
            }
        });
    }

    /**
     * 同意推荐商家审核
     *
     * @param request
     * @param response
     */
    @RequestMapping("/replaceBusinessPicture")
    public void replaceBusinessPicture(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String picture = getString(request, "picPath", "");
                return businessService.replaceBusinessPicture(id, picture);
            }
        });
    }

    /**
     * 订单商品详细
     *
     * @param request
     * @param response
     */
    @RequestMapping("/businessOrderDetail")
    public void businessOrderDetail(final HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                String orderNumber = getString(request, "orderNumber", "");
                return businessService.businessOrderDetail(orderNumber, nowPage, pageSize);
            }
        });
    }
}
